
#ifndef __CRankMgr_h__
#define __CRankMgr_h__

#include "commclass.h"
#include "../common/CRankArray.h" 
#include "MsgRank.pb.h"

class CZoneRankKey
{
public:
    CZoneRankKey()
    {
        Clear();
    }

    virtual void Clear()
    {
        _uiGID = 0;
        _iKey1 = 0;
        _i64Key2 = 0;
        _iKey3 = 0;
    }

    unsigned int _uiGID;
    int _iKey1;
    int64 _i64Key2;
    int _iKey3;  //时间
};

class CZoneRankBase : public CRankArray<CZoneRankKey, MAX_ZONE_RANK_COUNT>
{
public:
    virtual bool IsSame(CZoneRankKey& a, CZoneRankKey& b) const
    {
        return a._uiGID == b._uiGID;
    }
};

typedef CZoneRankBase T_ZONE_RANK_BASE;

class CZoneRank : public T_ZONE_RANK_BASE
{
public:
    virtual int CmpFunc(CZoneRankKey & a, CZoneRankKey & b) const
    {
        if (a._iKey1 != b._iKey1)
        {
            return (a._iKey1 > b._iKey1 ? 1 : -1);
        }        

        if (a._i64Key2 != b._i64Key2)
        {
            return (a._i64Key2 > b._i64Key2 ? 1 : -1);
        }

        if (a._iKey3 != b._iKey3)
        {
            return (a._iKey3 < b._iKey3 ? 1 : -1);    //时间越早,越靠前
        }

        if (a._uiGID != b._uiGID)
        {
            return (a._uiGID < b._uiGID ? 1 : -1);        //注册越早,越靠前
        }
        
        return 0;
    }
};

//排行榜刷新时间
#define RANK_REFRESH_TIME  1800

class CRankMgr
{
public:
    CRankMgr ()
    {
        _iNextClacTime = 0;
        _iNextTitleTime = 0;
    }

    void TimeTick (int iNowTime);

    int InitData ();

    //bCheckTime: 刷新榜前是否检查时间
    void RefreshAllRank (int iNowTime, bool bCheckTime = true);

    int GetRank (MsgReqRankOpt & stReq,  MsgAnsRankOpt & stAns);

    void CheckWhenLogIn (unsigned int uiGid, CRoleTitleData & stRoleTitle);

private:
    void GetRankData(ENUM_ZONE_RANK_TYPE enType, CZoneRankKey & stData, MsgZoneRankData& stRankData);

    void TriggerTitle (int iNowTime);

    void ClearRank ();

    //数据触发
    void LevelChange(unsigned int uiGID, short shNowLevel, int64 iNowExp);
    void FightChange(unsigned int uiGID, int iNowFight, short shNowLevel);
    void RepChange(unsigned int uiGID, int iNowRep, short shNowLevel);


private:
    int _iNextClacTime;
    int _iNextTitleTime;
    CTLib::CTArray<CZoneRank, ZONE_RANK_COUNT> _astAllRanks;  
};


#endif // __CRankMgr_h__

